from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver


class BasePage:
    # 基础设置
    

    def __init__(self, base_driver:WebDriver = None):
        
        self.desired_capabilities = {
            "platformName": "android",
            "deviceName": "127.0.0.1:7555",
            "appPackage": "com.tencent.wework",
            # "appActivity":"com.tencent.wework.launch.WwMainActivity",
            "appActivity": ".launch.WwMainActivity",
            "noReset": True,
            # 执行的时候不会重启app[实际结果为用例执行完后是否关闭app]
            "dontStopAppOnReset": True
        }
        
        if base_driver == None:
            self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", self.desired_capabilities)
            self.driver.implicitly_wait(20)

        else:
            self.driver = base_driver
            self.driver.implicitly_wait(20)

    # 封装 find_element()方法
    def find(self, by, locator=None):
            # 如果只传入一个元祖参数,需解包元组处理（find_element（）只支持2个参数，不支持元组）
        if locator is None:
            # *为解包元组
            return self.driver.find_element(*by)
            # 适配多种传参情景
        else:
            return self.driver.find_element(by=by, value=locator)

    # 进入消息页：默认页
    def goto_MessagePage(self):
        """
        进入通讯录菜单
        :return: ContactPage()
        """
        from AppWorkWxin.PO.defaultpage import DefaultPage
        # self.driver.find_element(MobileBy.XPATH, "//*[@text='通讯录']").click()
        self.find(MobileBy.XPATH, "//*[@text='消息']").click()
        return DefaultPage(self.driver)


    def goto_ContactPage(self):
        """
        进入通讯录菜单
        :return: ContactPage()
        """
        from AppWorkWxin.PO.contactpage import ContactPage
        # self.driver.find_element(MobileBy.XPATH, "//*[@text='通讯录']").click()
        self.find(MobileBy.XPATH, "//*[@text='通讯录']").click()
        return ContactPage(self.driver)




    def goto_WorkPlantPage(self):
        """
        进入工作台
        :return: WorkPlantPage
        """
        from AppWorkWxin.PO.workplantpage import WorkPlantPage
        self.find(MobileBy.XPATH, "//*[@text='工作台']").click()
        return WorkPlantPage(self.driver)

    def goto_MyPage(self):
        """
        进入我
        :return: MyPage
        """
        from AppWorkWxin.PO.mypage import MyPage
        self.find(MobileBy.XPATH, "//*[@text='我']").click()
        
        return MyPage(self.driver)